//
//  DialPlateView.m
//  BalanceCar
//
//  Created by 郑信鸿 on 17/6/27.
//  Copyright © 2017年 郑信鸿. All rights reserved.
//

#import "DialPlateView.h"

@implementation DialPlateView{
    
    UIImageView *_pointerView;
    UILabel *_speedLabel;
    CGFloat _maxSpeed;
    CGFloat _maxAngle;
    CGFloat _zeroSpeedAngle;
}

- (instancetype)init{
    
    if (self = [super init]) {
        _maxSpeed = 40.0;
          _maxAngle = M_PI * 4 / 3 ;
        _zeroSpeedAngle = -_maxAngle / 2;
        UIImageView *dialPate = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fun_dashboard"]];
        [self addSubview:dialPate];
        [dialPate mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(0);
        }];
        _pointerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fun_click"]];
        
        [self addSubview:_pointerView];
        [_pointerView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.center.mas_offset(0);
            
        }];
        _pointerView.transform = CGAffineTransformMakeRotation(_zeroSpeedAngle);
        [self estblishLabelView];
    }
    return self;
}

- (void)estblishLabelView{
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = XHLocalizedString(@"Current speed");
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textColor = [UIColor colorFromHexCode:@"#999999"];
    [self addSubview:titleLabel];
    UILabel *unitLabel = [[UILabel alloc] init];
    unitLabel.text = [SwitcherTool shareManager].unit;
    unitLabel.textAlignment = NSTextAlignmentCenter;
    unitLabel.font = [UIFont systemFontOfSize:14];
    unitLabel.textColor = [UIColor colorFromHexCode:@"#999999"];
    self.unitLabel = unitLabel;
    [self addSubview:unitLabel];
    _speedLabel = [[UILabel alloc] init];
    _speedLabel.text = @"0.0";
    _speedLabel.textAlignment = NSTextAlignmentCenter;
    _speedLabel.font = [UIFont boldSystemFontOfSize:23];
    _speedLabel.textColor = [UIColor blackColor];
    [self addSubview:_speedLabel];
    [unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.mas_offset(0);
        make.bottom.mas_offset(-45);
        make.height.mas_offset(12);
    }];
    [_speedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.mas_offset(0);
        make.bottom.mas_equalTo(unitLabel.mas_top).offset(-2);
        make.height.mas_offset(30);
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.mas_offset(0);
        make.bottom.mas_equalTo(_speedLabel.mas_top).offset(-2);
        make.height.mas_equalTo(12);
    }];
    
}

- (void)setSpeed:(CGFloat)speed{
    _speed = speed;
    _speedLabel.text = [NSString stringWithFormat:@"%0.1f",speed];
    [self configPointerTrangle];
    
}

- (void)configPointerTrangle{
    CGFloat willTureAngle = _maxAngle * (self.speed / _maxSpeed);
    CGFloat rightAngle = willTureAngle + _zeroSpeedAngle;
    [UIView animateWithDuration:0.5 animations:^{
        
        _pointerView.transform = CGAffineTransformMakeRotation(rightAngle);
        
    } completion:^(BOOL finished) {
        
    }];
  
}


@end
